# frozen_string_literal: true

require '../common/vagrant-common'

Vagrant.configure(2) do |config|
  config.vm.box = 'bento/debian-9'

  # Make it so that network access from the vagrant guest is able to
  # use SSH private keys that are present on the host without copying
  # them into the VM.
  config.ssh.forward_agent = true

  config.vm.hostname = 'gpdbvagrant'

  config.vm.provider :virtualbox do |vb|
    vb.customize [:modifyvm, :id, '--memory', 4096]
    cpu_count = 2

    # Determine the available cores in host system.
    # This mostly helps on linux, but it couldn't hurt on MacOSX.
    if RUBY_PLATFORM.match?(/linux/)
      cpu_count = `nproc`.to_i
    elsif RUBY_PLATFORM.match?(/darwin/)
      cpu_count = `sysctl -n hw.ncpu`.to_i
    end

    # Assign additional cores to the guest OS.
    vb.customize [:modifyvm, :id, '--cpus', cpu_count]
    vb.customize [:modifyvm, :id, '--cpuexecutioncap', '50']
    vb.customize [:modifyvm, :id, '--ioapic', 'on']

    # This setting makes it so that network access from inside the
    # vagrant guest is able to resolve DNS using the hosts VPN connection.
    vb.customize [:modifyvm, :id, '--natdnshostresolver1', 'on']
  end

  process_vagrant_local(config)

  config.vm.define(:gpdb) do |gpdb|
    name_vm config, 'gpdb-debian-dev-host'
    gpdb.vm.provision(:shell, path: 'vagrant-setup.sh', args: '--setup-gporca')
    gpdb.vm.provision(:shell,
                      path: '../common/vagrant-build-gporca.sh',
                      privileged: false)
    gpdb.vm.provision(:shell,
                      path: '../common/vagrant-build-gpdb.sh',
                      privileged: false,
                      env: current_git_data,
                      args: gpdb_build_args)
  end

  config.vm.define(:gpdb_without_gporca) do |gpdb|
    name_vm config, 'gpdb-without-gporca-debian-dev-host'
    gpdb.vm.provision(:shell, path: 'vagrant-setup.sh')
    gpdb.vm.provision(:shell,
                      path: '../common/vagrant-build-gpdb.sh',
                      privileged: false,
                      env: current_git_data,
                      args: gpdb_build_args(with_gporca: false))
  end
end
